package com.czy.owner.ui.setting;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.permission.Acp;
import com.czy.owner.permission.AcpListener;
import com.czy.owner.permission.AcpOptions;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.login.LoginActivity;
import com.czy.owner.utils.ActivityHelper;
import com.czy.owner.utils.GetDeviceId;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.widget.MyEditText;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2016/5/23.
 * 修改登录密码
 */

public class UpdateLoginPasswordActivity extends BaseActivity {
    @BindView(R.id.et_current_password)
    MyEditText etCurrentPassword;
    @BindView(R.id.et_new_password)
    MyEditText etNewPassword;
    @BindView(R.id.et_sure_new_password)
    MyEditText etSureNewPassword;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_update_login_password;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.update_login_password);
    }

    @Override
    protected void updateViews() {

    }


    @OnClick(R.id.btn_sure_update)
    public void sureUpdateOnClick(View view) {
        String current_password = etCurrentPassword.getText().toString().trim();
        String new_password = etNewPassword.getText().toString().trim();
        String new_sure_password = etSureNewPassword.getText().toString().trim();

        if (!PhoneUtils.isNetworkConnected(UpdateLoginPasswordActivity.this)) {
            return;
        }

        if (TextUtils.isEmpty(current_password)) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.current_password_no));
            etCurrentPassword.requestFocus();
            return;
        }

        if (current_password.length() < 6 || current_password.length() > 20) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.input_psd_length));
            etCurrentPassword.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(new_password)) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.new_password_no));
            etNewPassword.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(new_sure_password)) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.new_password_no));
            etSureNewPassword.requestFocus();
            return;
        }

        if (!new_password.equals(new_sure_password)) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.twice_new_password_disagree));
            etSureNewPassword.requestFocus();
            return;
        }


        if (new_sure_password.equals(current_password)) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.new_password_and_current_error));
            return;
        }

        if (new_password.length() < 6 || new_password.length() > 20) {
            PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, getString(R.string.input_psd_length));
            return;
        }

        if ((Build.VERSION.SDK_INT >= 23)) {
            phoneStatePermissions(current_password, new_password);
        } else {
            GetDeviceId getDeviceId = new GetDeviceId(UpdateLoginPasswordActivity.this);
            pid = getDeviceId.getCombinedId();
            MyLog.e("yang", "修改密码pid==" + pid);
            updatePassword(current_password, new_password);
        }

    }

    private void updatePassword(String current_password,String new_password){
        mLoadView.ShowLoadView();
        RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.UPDATE_LOGIN_API);
        params.addBodyParameter("phone", UserHelper.getInstance().getUserInfoModel(UpdateLoginPasswordActivity.this).getPhone());
        params.addBodyParameter("oldPassword", current_password);
        params.addBodyParameter("newPassword", new_password);
        params.addBodyParameter("pid", pid);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String data = PhoneUtils.checkResponseFlag(UpdateLoginPasswordActivity.this, result);
                if (data != null) {
                    ActivityHelper.getInstance().finishAllActivity();
                    startActivity(new Intent(UpdateLoginPasswordActivity.this, LoginActivity.class));
                    PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, "密码修改成功");
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });

    }


    public String pid = "";
    public void phoneStatePermissions(final String current_password, final String new_password) {
        Acp.getInstance(UpdateLoginPasswordActivity.this).request(new AcpOptions.Builder().setPermissions(Manifest.permission.READ_PHONE_STATE).build(),
                new AcpListener() {
                    @Override
                    public void onGranted() {
                        GetDeviceId getDeviceId = new GetDeviceId(UpdateLoginPasswordActivity.this);
                        pid = getDeviceId.getCombinedId();
                        MyLog.e("yang", "修改密码pid==" + pid);
                        updatePassword(current_password, new_password);
                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        PhoneUtils.ShowToastMessage(UpdateLoginPasswordActivity.this, "获取手机权限拒绝");

                    }
                });
    }

}
